package com.controller;

import com.bean.Activity;
import com.bean.User;
import com.github.pagehelper.PageHelper;
import com.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import java.util.List;

import static com.github.pagehelper.util.MetaObjectUtil.method;

/**
 * Created by Yuicon on 2017/3/20.
 */
@RestController
@CrossOrigin(origins = {"*"}, methods = {RequestMethod.OPTIONS, RequestMethod.POST, RequestMethod.GET})
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    private ActivityService activityService;


    @RequestMapping(value = "{activityId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    public Activity findOne(@PathVariable String activityId) {
        return activityService.findOne(activityId);
    }

    /**
     * 活动的添加action
     *
     * @param activity  需要添加的活动实例
     * @return 添加成功数量
     */
    @RequestMapping(value = "secure/insert", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    int insert(@RequestBody Activity activity, ServletRequest req) {
        return activityService.insert(activity, req);
    }

    /**
     * 活动的删除action
     *
     * @param activityId
     * @param req
     * @return
     */
    @RequestMapping(value = "secure/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    int delete(@RequestBody String activityId, ServletRequest req) {
        return activityService.deleteById(activityId, req);
    }

    /**
     * 获取当前用户的活动列表
     *
     * @param req
     * @return
     */
    @RequestMapping(value = "secure/myActivities", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    List<Activity> myActivities(@RequestBody int page, ServletRequest req) {
        PageHelper.startPage(page,5);
        return activityService.findByUserId(req);
    }

    /**
     * 获取活动列表
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    List<Activity> activities(int page) {
        PageHelper.startPage(page,9);//第一个参数是第几页；第二个参数是每页显示条数。
        return activityService.findAll();
    }

    /**
     * 更新活动信息
     *
     * @param activity
     * @param req
     * @return
     */
    @RequestMapping(value = "secure/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    int update(@RequestBody Activity activity, ServletRequest req) {
        return activityService.update(activity, req);
    }

    /**
     * 用户参加活动
     *
     * @param activityId
     * @param req
     * @return
     */
    @RequestMapping(value = "secure/campaign", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    int campaign(@RequestBody String activityId, ServletRequest req) {
        return activityService.campaign(activityId, req);
    }

    /**
     * 获取当前活动的参与用户列表
     *
     * @param activityId
     * @return
     */
    @RequestMapping(value = "campaignUsers", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    List<User> campaignUsers(String activityId) {
        return activityService.getUsersByActivityId(activityId);
    }

    /**
     * 获取当前用户参加的活动列表
     *
     * @param req
     * @return
     */
    @RequestMapping(value = "secure/campaignActivities", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    List<Activity> campaignActivities(ServletRequest req) {
        return activityService.getActivityByUserId(req);
    }



}
